# coding:utf-8
"""
description:Mode和Schedule的Public页面
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG, logger
from config.GlobalParameter import TEST_Element_YAML
from pages.Dashboard_page import dashboard_page

Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'ModeSchedulePublic.yaml'))


class ModeSchedulePublic_page(dashboard_page):
    by = mobileby.MobileBy()

    """mode、schedule and countdown添加按钮，右上角+号"""
    btnDeviceSchedulingAdd_loc = (by.ID, Data.get_elementInfo(0))

    def click_btnDeviceSchedulingAdd(self):
        """mode、schedule and countdown添加按钮，右上角+号"""
        self.find_element(*self.btnDeviceSchedulingAdd_loc).click()

    """mode、schedule and countdown已添加列表项的模块"""
    SchedulingAddedList_loc = (by.ID, Data.get_elementInfo(1))

    def assert_SchedulingAddedList(self):
        """mode、schedule and countdown已添加列表项的模块"""
        return self.assertVisible(*self.SchedulingAddedList_loc)

    def leftSwipeClickDel(self,StartElement,EndElement):
        """
        从switch向左滑倒mode name 元素，出现Delete按钮点击后跳转页面
        :return:
        """
        try:
            # 获取switch元素坐标
            centre = gesture_mainpulation().centreXAndY(StartElement[0])
            # 获取mode name元素坐标
            centre2 = gesture_mainpulation().centreXAndY(EndElement[0])
            # 从switch向左滑倒mode name 元素
            self.driver.swipe(centre[0], centre[1], centre2[0], centre2[1])
            """根据switch所在坐标推算Delete坐标并点击"""
            self.driver.tap([(centre[0], centre[1])], 100)
        except:
            LOG.error("滑动失败，检查被测item的坐标是否能正常操作。。。")

    """Mode item的mode name"""
    ModeName_loc = (by.ID, Data.get_elementInfo(2))

    def get_ModeName(self):
        """Mode item的mode name"""
        return self.find_elements(*self.ModeName_loc)

    """mode time，格式：06:00 - 09:00"""
    ModeTime_loc = (by.ID, Data.get_elementInfo(3))

    def get_ModeTime(self):
        """mode time，格式：06:00 - 09:00"""
        return self.find_elements(*self.ModeTime_loc)

    """mode repeat，根据选择日期显示为Sunday, 
    Mon、Tue、Wed、Thu、Fri、Sat、once、weekends、 weekdays、everyday"""
    ModeRepeat_loc = (by.ID, Data.get_elementInfo(4))

    def get_ModeRepeat(self):
        """mode repeat，根据选择日期显示为Sunday, 
        Mon、Tue、Wed、Thu、Fri、Sat、once、weekends、 weekdays、everyday"""
        return self.find_elements(*self.ModeRepeat_loc)

    """mode switch，text：on和off"""
    switchMode_loc = (by.ID, Data.get_elementInfo(5))

    def get_switchMode(self):
        """mode switch，text：on和off"""
        return self.find_elements(*self.switchMode_loc)

    """Schedule time，格式：12:30"""
    ScheduleTime_loc = (by.ID, Data.get_elementInfo(6))

    def get_ScheduleTime(self):
        """Schedule time，格式：12:30"""
        return self.find_elements(*self.ScheduleTime_loc)

    """Schedule Action，ON和OFF，"""
    ScheduleAction_loc = (by.ID, Data.get_elementInfo(7))

    def get_ScheduleAction(self):
        """Schedule Action，ON和OFF，"""
        return self.find_elements(*self.ScheduleAction_loc)

    """Schedule repeat，根据选择日期显示为Sunday, 
    Mon、Tue、Wed、Thu、Fri、Sat、once、weekends、 weekdays、everyday"""
    ScheduleRepeat_loc = (by.ID, Data.get_elementInfo(8))

    def get_ScheduleRepeat(self):
        """Schedule repeat，根据选择日期显示为Sunday,
        Mon、Tue、Wed、Thu、Fri、Sat、once、weekends、 weekdays、everyday"""
        return self.find_elements(*self.ScheduleRepeat_loc)

    """schedule switch,打开或者关闭，text：on和off"""
    switchSchedule_loc = (by.ID, Data.get_elementInfo(9))

    def get_switchSchedule(self):
        """schedule switch,打开或者关闭，text：on和off"""
        return self.find_elements(*self.switchSchedule_loc)

    """Countdown time，格式：23:00:00"""
    CountdownTime_loc = (by.ID, Data.get_elementInfo(10))

    def get_CountdownTime(self):
        """Countdown time，格式：23:00:00"""
        return self.find_elements(*self.CountdownTime_loc)

    """Countdown Action，ON和OFF，plug用"""
    CountdownAction_loc = (by.ID, Data.get_elementInfo(11))

    def get_CountdownAction(self):
        """Countdown Action，ON和OFF，plug用"""
        return self.find_elements(*self.CountdownAction_loc)

    """Countdown switch,打开或者关闭，text：on和off"""
    switchCountdown_loc = (by.ID, Data.get_elementInfo(12))

    def get_switchCountdown(self):
        """Countdown switch,打开或者关闭，text：on和off"""
        return self.find_elements(*self.switchCountdown_loc)

    """Plug Control page>点击Mode 按钮"""
    ControlMode_loc = (by.ID, Data.get_elementInfo(13))

    def click_ControlMode(self):
        """Plug Control page>点击Mode 按钮"""
        if self.assertVisible(*self.ControlMode_loc):
            LOG.info("点击Smart Plug UK|US Control page的Mode按钮...")
            self.find_element(*self.ControlMode_loc).click()
        else:
            LOG.info("点击1/2/3-Gang 、Dimmer、3.5kw High、Curtain、"
                     "Roller Blind和Ceiling Light Control page的Mode按钮...")
            self.find_element(*self.ControlMode2_loc).click()

    """Plug Control page>点击Schedule按钮"""
    ControlSchedule_loc = (by.ID, Data.get_elementInfo(14))

    def click_ControlSchedule(self):
        """Plug Control page>点击Schedule按钮"""
        if self.assertVisible(*self.ControlSchedule_loc):
            LOG.info("点击Smart Plug UK|US Control page的Schedule按钮》》》")
            self.find_element(*self.ControlSchedule_loc).click()
        else:
            LOG.info("点击1/2/3-Gang 、Dimmer、3.5kw High、Curtain、"
                     "Roller Blind和Ceiling Light Control page的Schedule按钮》》》")
            self.find_element(*self.ControlSchedule2_loc).click()

    """Plug Control page>点击Add Countdown按钮"""
    ControlCountdown_loc = (by.ID, Data.get_elementInfo(15))

    def click_ControlCountdown(self):
        """Plug Control page>点击Add Countdown按钮"""
        self.find_element(*self.ControlCountdown_loc).click()

    """1/2/3-Gang 、Dimmer、3.5kw High、Curtain、Roller Blind
    和Ceiling Light Control page>点击Mode按钮"""
    ControlMode2_loc = (by.ID, Data.get_elementInfo(16))

    """1/2/3-Gang 、Dimmer、3.5kw High、Curtain、Roller Blind
    和Ceiling Light Control page>点击Schedule 按钮"""
    ControlSchedule2_loc = (by.ID, Data.get_elementInfo(17))

    """Add and Edit page>点击Mode一行后的选项，Morning"""
    SelectMode1_loc = (by.ID, Data.get_elementInfo(18))

    """Add and Edit page>点击Mode一行后的选项，Evening"""
    SelectMode2_loc = (by.ID, Data.get_elementInfo(19))

    """Add and Edit page>点击Mode一行后的选项，Sleep"""
    SelectMode3_loc = (by.ID, Data.get_elementInfo(20))

    def click_SelectMode(self):
        """Add and Edit page>随机选择mode"""
        ran = random.randint(1, 3)
        print(ran)
        if ran == 1:
            LOG.info("Add and Edit Mode Page>随机选择 Morning mode...")
            self.find_element(*self.SelectMode1_loc).click()
        elif ran == 2:
            LOG.info("Add and Edit Mode Page>随机选择 Evening mode...")
            self.find_element(*self.SelectMode2_loc).click()
        else:
            LOG.info("Add and Edit Mode Page>随机选择 Sleep mode...")
            self.find_element(*self.SelectMode3_loc).click()
        super().click_ClosePopup()  # 点击”x“按钮就是确认选项

    """滑动已添加列表的页面，进行刷新数据"""
    swipeDeviceSchedules_loc = (by.ID, Data.get_elementInfo(21))

    def swipe_listPage(self):
        """滑动已添加列表的页面，进行刷新数据"""
        gesture_mainpulation().swipe_down(self.driver, self.find_element(*self.swipeDeviceSchedules_loc))
